(** 
    HttpRequest.ml
    See httpRequest.mli for more details about the functions.

    Bozman Çagdas <bozman.cagdas@gmail.com> - 2011
    Razakamiadana Tefin <tefy.razaka@gmail.com>   - 2011
*)
open Utils

exception IO_error of string

let string_of_url url =
  let connection = Curl.init () and
	buf = Buffer.create 1763 in
  let _ = 
    Curl.set_writefunction 
      connection
      (fun x -> Buffer.add_string buf x; String.length x) 
  in    
  let _ = 
    Curl.setopt connection
      (Curl.CURLOPT_USERAGENT ("User-Agent: Wikaml/0.1")) in
  let _ = Curl.set_url connection url in
  let rec aux () =
    try
      let _ = Curl.perform connection in
      let _ = Curl.cleanup connection in
      Buffer.contents buf
    with 
      | Curl.CurlException (Curl.CURLE_COULDNT_RESOLVE_HOST, _, _) -> 
      (* Wait 10 seconds and then try again *)
	debug ("Trying again [%s] ...\n") url; flush stderr;
	Unix.sleep 10;
	aux ();
      | _ -> raise (IO_error url)
  in
  aux ()
